package server.content.skilling.fishing;

import core.util.Misc;
import server.content.skilling.PlayerSkill;
import server.event.CycleEvent;
import server.event.CycleEventContainer;
import server.event.CycleEventHandler;
import server.game.players.Client;

/**
 * 
 * @author Miracle
 * 
 */
public class MiracleFishing extends PlayerSkill {

	public int fishingType = -1;
	public boolean canFish = true;

	public MiracleFishing(Client c, int skillId) {
		super(c, skillId);
		setTick(2);
	}

	@Override
	public void init() {
		if (!canFish) {
			kill();
			sendMessage("Fishing has been currently disabled by an Adminstrator.");
			return;
		}
		if (invIsFull()) {
			sendMessage("Your inventory is to full too catch anymore fish.");
			kill();
			return;
		}
		if (System.currentTimeMillis() - getClient().lastThieve < 3000)
			return;
		switch (fishingType) {
		case 0: // Small Net
			if (!playerHasItem(303)) {
				kill();
				sendMessage("You need a Small Net to fish here.");
				return;
			}
			sendMessage("You attempt to catch some fish.");
			doAnimation(621);
			setActive(true);
			process();
			break;
		case 1: // Big Net

			break;
		case 2: // Bait
		case 3: // Lure
			if (getPlayerLevel() <= 19) {
				kill();
				sendMessage("You need a fishing level of atleast 20 to fish here.");
				return;
			}
			if (playerHasItem(309) && playerHasItem(314, 3)) {
				sendMessage("You attempt to catch some fish.");
				doAnimation(622);
				setActive(true);
				process();
			} else {
				sendMessage("You need a Fly Fishing Rod and atleast 3 Feathers to fish here.");
				kill();
				return;
			}
			break;
		case 4: // Cage
			if (getPlayerLevel() < 40) {
				sendMessage("You need a fishing level of 40 to catch Lobsters..");
				kill();
				return;
			}
			if (!playerHasItem(301)) {
				sendMessage("You need a Lobster Pot to fish here.");
				kill();
				return;
			}
			sendMessage("You attempt to catch some fish.");
			setActive(true);
			doAnimation(619);
			process();
			break;
		case 5: // Harpoon
			if (getPlayerLevel() <= 34) {
				kill();
				sendMessage("You need a fishing level of atleast 35 to fish here.");
				return;
			}
			if (!playerHasItem(311)) {
				kill();
				sendMessage("You need a Harpoon to fish here.");
				return;
			}
			sendMessage("You attempt to catch some fish.");
			setActive(true);
			doAnimation(618);
			process();
			break;
		}
		getClient().lastThieve = System.currentTimeMillis();
	}

	@Override
	public void process() {
		CycleEventHandler.getSingleton().addEvent(getClient(),
				new CycleEvent() {

					@Override
					public void execute(CycleEventContainer container) {
						if (isActive()) {
							if (invIsFull()) {
								sendMessage("Your inventory is to full too catch anymore fish.");
								kill();
								return;
							}
							switch (fishingType) {
							case 0:
								if (!playerHasItem(303)) {
									kill();
									sendMessage("You need a Small Net to fish here.");
									return;
								}
								doAnimation(621);
								if (Misc.random(120) < (getFishChance() - getFishDifficulty())) {
									if (getPlayerLevel() >= 15) {
										final int randFish = Misc.random(10);
										if (randFish == 1 || randFish == 5
												|| randFish == 8
												|| randFish == 4) {
											addItem(321, 1);
											addXP(120, getSkillId());
											getClient()
													.sendMessage(
															"You manage to catch a Raw Anchovie.");
										} else {
											addItem(317, 1);
											addXP(30, getSkillId());
											getClient()
													.sendMessage(
															"You manage to catch a Raw Shrimp.");
										}
									} else if (getPlayerLevel() >= 1
											&& getPlayerLevel() <= 14) {
										addItem(317, 1);
										addXP(30, getSkillId());
										getClient()
												.sendMessage(
														"You manage to catch a Raw Shrimp.");
									}
								}
								break;
							case 3:
								if (!playerHasItem(314, 3)) {
									kill();
									sendMessage("You need atleast 3 feathers to fish here.");
									return;
								}
								if (!playerHasItem(309)) {
									kill();
									sendMessage("You need a Fly Fishing Rod to fish here.");
									return;
								}
								doAnimation(622);
								if (Misc.random(120) < (getFishChance() - getFishDifficulty())) {
									if (getPlayerLevel() >= 30) {
										final int randFish = Misc.random(8);
										if (randFish == 1 || randFish == 5
												|| randFish == 8) {
											addItem(331, 1);
											addXP(65, getSkillId());
											getClient()
													.sendMessage(
															"You manage to catch a Raw Salmon.");
											deleteItem(314, 1);
										} else {
											addItem(335, 1);
											addXP(210, getSkillId());
											getClient()
													.sendMessage(
															"You manage to catch a Raw Trout.");
											deleteItem(314, Misc.random(3));
										}
									} else if (getPlayerLevel() <= 29
											&& getPlayerLevel() >= 20) {
										addItem(150, 1);
										addXP(45, getSkillId());
										getClient()
												.sendMessage(
														"You manage to catch a Raw Trout.");
										deleteItem(314, Misc.random(3));
									}
								}
								break;
							case 4:
								if (!playerHasItem(301)) {
									sendMessage("You need a Lobster Pot to fish here.");
									kill();
									return;
								}
								doAnimation(619);
								if (Misc.random(120) < (getFishChance() - getFishDifficulty())) {
									addItem(377, 1);
									sendMessage("You manage to catch a Raw Lobster.");
									addXP(270, getSkillId());
								}
								break;
							case 5:
								if (!playerHasItem(311)) {
									kill();
									sendMessage("You need a Harpoon to fish here.");
									return;
								}
								doAnimation(618);
								if (Misc.random(120) < (getFishChance() - getFishDifficulty())) {
									if (getPlayerLevel() >= 76) {
										final int randFish = Misc.random(10);
										if (randFish == 1 || randFish == 5
												|| randFish == 8
												|| randFish == 4) {
											addItem(383, 1);
											addXP(330, getSkillId());
											sendMessage("You manage to catch a Raw Shark.");
										} else {
											addItem(359, 1);
											addXP(210, getSkillId());
											sendMessage("You manage to catch a Raw Tuna.");
										}
									} else if (getPlayerLevel() >= 35
											&& getPlayerLevel() <= 75) {
										addItem(359, 1);
										addXP(210, getSkillId());
										sendMessage("You manage to catch a Raw Tuna.");
									}
								}
								break;
							}
						} else {
							container.stop();
						}
					}

					@Override
					public void stop() {

					}
				}, getTick());
	}

	@Override
	public void kill() {
		fishingType = -1;
		setActive(false);
		resetAnim();
	}

	public final int getFishChance() {
		final int lvl = getPlayerLevel();
		if (lvl >= 1 && lvl <= 9)
			return 4;
		else if (lvl >= 10 && lvl <= 19)
			return 8;
		else if (lvl >= 20 && lvl <= 29)
			return 12;
		else if (lvl >= 30 && lvl <= 39)
			return 16;
		else if (lvl >= 40 && lvl <= 49)
			return 20;
		else if (lvl >= 50 && lvl <= 59)
			return 24;
		else if (lvl >= 60 && lvl <= 69)
			return 28;
		else if (lvl >= 70 && lvl <= 79)
			return 32;
		else if (lvl >= 80 && lvl <= 89)
			return 34;
		else if (lvl >= 90 && lvl <= 99)
			return 40;
		return 0;
	}

	public final int getFishDifficulty() {
		switch (fishingType) {
		case 0:
			return Misc.random(1);
		case 3:
			return Misc.random(2);
		case 4:
			return Misc.random(4);
		case 5:
			return Misc.random(7);
		}
		return 0;
	}

}
